package com.simple.editor.action.impl;

import com.simple.editor.ApplicationContext;
import com.simple.editor.action.EditorException;
import com.simple.editor.callback.CallbackListener;
import com.simple.editor.callback.DefaultUICallbackImpl;
import com.simple.editor.ui.ChangesPane;
import com.simple.editor.ui.FileModel;
import com.simple.editor.ui.FileTreePane;
import com.simple.editor.ui.TabbedEditorPane;
import com.simple.editor.util.UIUtilities;
import com.simple.editor.vcs.VcsFile;
import com.simple.editor.vcs.VcsStatus;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: vsoroka
 * Date: 01.07.13
 * Time: 13:51
 * To change this template use File | Settings | File Templates.
 */
public class RefreshActionUICallbackImpl extends DefaultUICallbackImpl {
    private final FileModel[] files;
    private final Map<String, VcsFile> vcsFilesMap;
    private final ApplicationContext ctx;
    private final CallbackListener callbackListener;
    private final boolean syncEditor;


    public RefreshActionUICallbackImpl(
            Map<String, VcsFile> vcsFilesMap, CallbackListener callbackListener,
            ApplicationContext ctx, FileModel... files) {
        this(vcsFilesMap, callbackListener, ctx, false, files);
    }

    public RefreshActionUICallbackImpl(
            Map<String, VcsFile> vcsFilesMap, CallbackListener callbackListener,
            ApplicationContext ctx, final boolean syncEditor, FileModel... files) {
        super("Refreshing status", callbackListener);
        this.callbackListener = callbackListener;
        this.files = files;
        this.vcsFilesMap = vcsFilesMap;
        this.ctx = ctx;
        this.syncEditor = syncEditor;
    }

    @Override
    public void onComplete() {
        UIUtilities.invoke(new Runnable() {
            @Override
            public void run() {
                for (final FileModel file : files) {
                    // update UI model vcs file status
                    file.setStatus(vcsFilesMap.get(file.getAbsolutePath()).getStatus());

                    final boolean isOpened = ctx.getComponent(TabbedEditorPane.class).isOpened(file.getAbsolutePath());

                    if (!isOpened && file.getStatus() != VcsStatus.DELETED) {
                        // if file not opened and not deleted - remove from changes view
                        ctx.getComponent(ChangesPane.class).removeFromChangesView(file.getAbsolutePath());
                    } else {
                        ctx.getComponent(TabbedEditorPane.class).syncLabelStyle(file);
                        ctx.getComponent(ChangesPane.class).addToChanges(file);
                    }
                    if (isOpened && syncEditor) {
                        // if file opened refresh tab
                        // TODO ask user if file was not saved
                        try {
                            ctx.getComponent(TabbedEditorPane.class).syncEditorFromDisk(file);
                        } catch (EditorException ex) {
                            callbackListener.addError(String.format("File read error: %s", ex.getMessage()));
                        }
                    }
                }
                ctx.getComponent(FileTreePane.class).refreshTree();
                ctx.getComponent(ChangesPane.class).refreshTree();
                callbackListener.addInfo("Refresh completed.");
            }
        });
    }

    @Override
    public void onFailure(final Exception ex) {
        UIUtilities.invoke(new Runnable() {
            @Override
            public void run() {
                // update UI model vcs file status
                for (FileModel file : files) {
                    file.setStatus(vcsFilesMap.get(file.getAbsolutePath()).getStatus());
                }
                ctx.getComponent(TabbedEditorPane.class).syncLabelStyle(files);
                ctx.getComponent(ChangesPane.class).refreshTree();
                callbackListener.addError(String.format("Refresh error: %s", ex.getMessage()));
            }
        });
    }

}
